/*
 * Copyright @ 苏州瑞云信息技术有限公司 All rights reserved.
 */
package cn.com.recloud.bbs.service.impl;

import cn.com.recloud.bbs.constant.MetaOptionConstant;
import cn.com.recloud.bbs.entity.bbs_campaign;
import cn.com.recloud.bbs.entity.bbs_contact;
import cn.com.recloud.bbs.dao.ContactDao;
import cn.com.recloud.bbs.entity.metaoption;
import cn.com.recloud.bbs.service.ContactService;
import cn.com.recloud.bbs.service.MetaoptionService;
import cn.com.recloud.one.spring.service.impl.BaseServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author keblea xie
 * @date 2020-06-08
 */
@Service
public class ContactServiceImpl extends BaseServiceImpl<ContactDao, bbs_contact> implements ContactService {
    @Resource
    ContactDao contactDao;
    @Resource
    MetaoptionService metaoptionService;

    /**
     * 修改昵称
     * @param bbsContact
     * @return
     */
    @Override
    public boolean updateName(bbs_contact bbsContact) {
        return super.updateById(bbsContact);
    }

    /**
     * 保存或更新联系人信息
     * @param bbsContact
     * @return
     */
    @Override
    public bbs_contact updateOrSaveContact(bbs_contact bbsContact) {
        String bbsAccountCode = bbsContact.getBbs_account_code();
        String code = bbsContact.getCode();
        //用户来源
        metaoption option = metaoptionService.findMetaOptionBySetNameAndValue(MetaOptionConstant.USER_SOURCE, "10");
        bbsContact.setSource(option.getValue());
        bbsContact.setSourcename(option.getName());
        LambdaQueryWrapper<bbs_contact> lambda = new QueryWrapper<bbs_contact>().lambda();
        lambda.eq(bbs_contact::getBbs_account_code,bbsAccountCode)
                .eq(bbs_contact::getCode,code);
        bbs_contact bbsContact1 = contactDao.selectOne(lambda);
        //根据租户code和用户code判断用户唯一性
        if(null == bbsContact1){
            //默认将名称设置为昵称
            if (StringUtils.isBlank(bbsContact.getNickname())){
                bbsContact.setNickname(bbsContact.getName());
            }
            super.save(bbsContact);
        }else{
            if (StringUtils.isBlank(bbsContact.getNickname())){
                bbsContact.setNickname(bbsContact1.getNickname());
            }
            bbsContact.setBbs_contactid(bbsContact1.getBbs_contactid());
            super.updateById(bbsContact);
        }
        return bbsContact;
    }
}
